package com.eastfair.venuebooking.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.venuebooking.dto.*;
import com.eastfair.venuebooking.entity.Space;
import com.eastfair.venuebooking.entity.SpaceCategory;
import com.eastfair.venuebooking.service.SpaceCategoryService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.venuebooking.vo.SpaceCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 场地分类表
 * </p>
 *
 * @author linan
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/spaceCategory")
@Api(value = "SpaceCategory", tags = "场地分类表")
@PreAuth(replace = "venuebooking:spaceCategory:")
public class SpaceCategoryController extends SuperController<SpaceCategoryService, Long, SpaceCategory, SpaceCategoryPageQuery, SpaceCategorySaveDTO, SpaceCategoryUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private  SpaceCategoryService spaceCategoryService;

    @Override
    public void handlerResult(IPage<SpaceCategory> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<SpaceCategory> spaceCategoryList = list.stream().map((map) -> {
            SpaceCategory spaceCategory = SpaceCategory.builder().build();
            //TODO 请在这里完成转换
            return spaceCategory;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(spaceCategoryList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<SpaceCategoryPageQuery> params) {
        SpaceCategoryPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }


    /**
     * 展厅分类列表
     * @param spaceCategoryPageQuery
     * @return
     */
    @ApiOperation(value = "展厅分类列表")
    @PostMapping(value = "/querySpaceCategoryList")
    public R<List<SpaceCategoryVO>> querySpaceCategoryList(@RequestBody SpaceCategoryPageQuery spaceCategoryPageQuery) {
      return R.success(spaceCategoryService.querySpaceCategoryList(spaceCategoryPageQuery));

    }

    /**
     * 添加展厅分类
     * @param spaceCategorySaveDTO
     * @return
     */
    @ApiOperation(value = "添加展厅分类")
    @SysLog("添加展厅分类")
    @PostMapping(value = "/saveSpaceCategoryInfo")
    public R<Boolean> saveSpaceCategoryInfo (@RequestBody @Validated(SpaceCategorySaveDTO.Save.class) SpaceCategorySaveDTO spaceCategorySaveDTO) {

        return R.success(spaceCategoryService.saveSpaceCategoryInfo(spaceCategorySaveDTO));
    }

    /**
     * 修改展厅分类
     * @param spaceCategoryUpdateDTO
     * @return
     */
    @ApiOperation(value = "修改展厅分类")
    @SysLog("修改展厅分类")
    @PostMapping(value = "/updateSpaceCategoryInfo")
    public R<Boolean> updateSpaceCategoryInfo (@RequestBody @Validated(SpaceCategoryUpdateDTO.Update.class) SpaceCategoryUpdateDTO spaceCategoryUpdateDTO) {

        return R.success(spaceCategoryService.updateSpaceCategoryInfo(spaceCategoryUpdateDTO));
    }

    /**
     * 逻辑删除展厅信息
     * @return
     */
    @ApiOperation(value = "逻辑删除展厅分类信息")
    @SysLog("删除展厅分类")
    @GetMapping(value = "/delSpaceCategoryInfoById/{id}")
    public R<Boolean> delSpaceCategoryInfoById(@PathVariable Long id){

        return R.success(spaceCategoryService.delSpaceCategoryInfoById(id));
    }



}
